﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Lenovo.CFI.Common;
using Lenovo.CFI.DAL;

namespace Lenovo.CFI.BLL
{
    public class UserIdeaAppBl
    {
        public List<UserIdeaApp> GetIdeaApp(string applicant, int? ideaID, string approver, DateTime? timeF, DateTime? timeT, int? status)
        {
            return UserIdeaAppDa.GetIdeaApp(applicant, ideaID, approver, timeF, timeT, status);
        }

        public UserIdeaApp GetIdeaAppByID(int id)
        {
            return UserIdeaAppDa.GetIdeaAppByID(id);
        }

        public void Apply(string applicant, int ideaID, string reason)
        {
            if (UserIdeaAppDa.CanAccess(applicant, ideaID)) return;
            
            UserIdeaApp app = new UserIdeaApp();
            app.Status = 1;
            app.Applicant = applicant;
            app.IdeaID = ideaID;
            app.AppReason = reason;
            app.AppTime = DateTime.Now;

            UserIdeaAppDa.InsertIdeaApp(app);
        }

        public void Agree(int appID, string approver, string comment)
        {
            UserIdeaApp app = UserIdeaAppDa.GetIdeaAppByID(appID);
            if (app == null) throw new BusinessObjectLogicException("The specified application does not exist!");

            if (app.Status != 1) throw new BusinessObjectLogicException("The specified application has been changed!");

            app.Status = 2;
            app.Approver = approver;
            app.ApproveReason = comment;

            using (TranscationHelper trans = TranscationHelper.GetInstance())
            {
                trans.BeginTrans();
                try
                {
                    UserIdeaAppDa.UpdateIdeaApp(app, trans);

                    UserIdeaAppDa.InsertUserIdeaList(app.Applicant, app.IdeaID, trans);

                    trans.CommitTrans();
                }
                catch
                {
                    trans.RollTrans();
                    throw;
                }
            }
        }

        public void Reject(int appID, string approver, string comment)
        {
            UserIdeaApp app = UserIdeaAppDa.GetIdeaAppByID(appID);
            if (app == null) throw new BusinessObjectLogicException("The specified application does not exist!");

            if (app.Status != 1) throw new BusinessObjectLogicException("The specified application has been changed!");

            app.Status = 3;
            app.Approver = approver;
            app.ApproveReason = comment;

            UserIdeaAppDa.UpdateIdeaApp(app);
        }

        public void Cancel(int appID)
        {
            UserIdeaApp app = UserIdeaAppDa.GetIdeaAppByID(appID);
            if (app == null) throw new BusinessObjectLogicException("The specified application does not exist!");

            if (app.Status != 1) throw new BusinessObjectLogicException("The specified application has been changed!");

            UserIdeaAppDa.DeleteIdeaApp(appID);
        }

        public bool CanAccess(string user, int ideaID)
        {
            return UserIdeaAppDa.CanAccess(user, ideaID);
        }

        public List<Idea> GetUserIdeaList(string user)
        {
            return UserIdeaAppDa.GetUserIdeaList(user);
        }

        public List<Common.Sys.User> GetIdeaUserList(int ideaID)
        {
            return UserIdeaAppDa.GetIdeaUserList(ideaID);
        }
    }
}
